package com.hoshiibuy.sys.permission.controller;

import java.time.LocalDateTime;
import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.common.utils.BeanUtils;
import com.hoshiibuy.sys.permission.entity.SysOrganization;
import com.hoshiibuy.sys.permission.entity.param.SysOrganizationInsertParam;
import com.hoshiibuy.sys.permission.entity.param.SysOrganizationQueryParam;
import com.hoshiibuy.sys.permission.entity.param.SysOrganizationUpdateParam;
import com.hoshiibuy.sys.permission.entity.vo.SysOrganizationDetailVO;
import com.hoshiibuy.sys.permission.entity.vo.SysOrganizationVO;
import com.hoshiibuy.sys.permission.service.ISysOrganizationService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 部门控制器
 * 
 * @author Mr.wu
 *
 */
@Api(tags = "部门接口")
@RestController
@RequestMapping("/dept")
@Slf4j
public class DeptController {

	@Autowired
	private ISysOrganizationService SysOrganizationService;

	@ApiOperation("新增部门")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Response addDept(@Valid SysOrganizationInsertParam param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		SysOrganization dept = new SysOrganization();
		BeanUtils.copyProperties(param, dept);
		// TODO 只接受指定参数
		// TODO 获取创建人信息
		dept.setCreatedby("admin");
		return SysOrganizationService.addDept(dept);
	}

	@ApiOperation("修改部门")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Response updateDept(@Valid SysOrganizationUpdateParam param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		SysOrganization dept = new SysOrganization();
		BeanUtils.copyProperties(param, dept);
		// TODO 获取创建人信息
		dept.setModifiedby("admin");
		dept.setModifiedtime(LocalDateTime.now());
		return SysOrganizationService.updateDept(dept);
	}

	@ApiOperation("删除部门")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Response deleteDept(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		return SysOrganizationService.deleteDept(id);
	}

	@ApiOperation("获取部门详情")
	@RequestMapping(value = "/getDetail", method = RequestMethod.GET)
	public Response<SysOrganizationDetailVO> getDeptDetail(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		return SysOrganizationService.getDetail(id);
	}

	@ApiOperation("获取部门列表")
	@RequestMapping(value = "getList", method = RequestMethod.GET)
	public Response<List<SysOrganizationVO>> getDeptList(@Valid SysOrganizationQueryParam param,
			BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		QueryWrapper<SysOrganization> queryWrapper = param.getQueryWrapper();
		return SysOrganizationService.getList(queryWrapper);
	}

	@ApiOperation("分页查询部门")
	@RequestMapping(value = "/getPage", method = RequestMethod.GET)
	public Response<IPage<SysOrganizationVO>> getDeptPage(@Valid SysOrganizationQueryParam param,
			BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		QueryWrapper<SysOrganization> queryWrapper = param.getQueryWrapper();
		String sqlSelect = BeanUtils.getSqlSelect(SysOrganizationVO.class);
		queryWrapper.select(sqlSelect);
		IPage<SysOrganization> page = new Page<>(Long.parseLong(param.getPageNumber()),
				Long.parseLong(param.getPageSize()));

		return SysOrganizationService.getPage(page, queryWrapper);
	}

}
